import { IDesignExportData, IExportParamModelData } from "@/types";

type Model = IExportParamModelData
type Point = [number, number]
interface Plank {
  name: string,
  textureName: string,
  thickness: number,
  points: Point[]
}

/** 遍历模型树 */
export function throughModel(model: Model, cb: (model: Model) => void) {
  const recursion = (model: Model) => {
    cb(model)
    model.subModels?.forEach(model => {
      recursion(model)
    })
  }
  recursion(model)
}

export function getPlankList(designExportData: IDesignExportData) {
  const plankList: Plank[] = []
  designExportData.paramModel.map((model) => {
    let parentModel: Model
    throughModel(model, (model) => {
      if (model.paramPlankPath) {
        const points = model.points as any
        if (!points) return
        plankList.push({
          name: parentModel.modelName,
          textureName: model.textureName,
          thickness: model.thickness,
          points: points.map(({ x, y }) => [x, y])
        })
      }
      parentModel = model
    })
  })
  return plankList
}